import {defineStore} from 'pinia'
import request from '@/utils/request'
import {ResApi,CategoryItem,TopCategory,SubCategory} from '@/types/data'
import {topCategory} from '@/store/constant'


const defaultCategory = topCategory.map((item)=>{
    return {
        name:item
    }
})

const useCategoryStore  = defineStore('category',{
    state(){
        return {
            // 所有分类列表
            list: defaultCategory as CategoryItem[],
            // 一级分类
            topCategory:{} as TopCategory,
            // 二级分类
            subCategory:{} as SubCategory
        }
    },
    actions:{
        async getAllCategory(){
           const res = await request.get<ResApi<CategoryItem[]>>('/home/category/head')
           this.list = res.data.result
        },

        async getTopCategory(id:string){
            const res = await request.get<ResApi<TopCategory>>('/category',{params:{id}})
            this.topCategory = res.data.result
        },

        async getSubCategory(id:string){
            const res = await request.get<ResApi<SubCategory>>('/category/sub/filter',{params:{id}})
            this.subCategory = res.data.result
        }
    },
    getters:{}
})


export default useCategoryStore